﻿using iS3.DAL.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace iS3.DAL
{
    public class Capacity
    {
        public List<DomainBase> domains;
        public List<DGEntityStruct> entities;
        public List<HalfStructTemplate> halfStructTemplates;
        public List<FileTemplate> fileTemplates;
        public List<D2ResourceTemplate> d2ResourceTemplates;
        public List<D3ResourceTemplate> d3ResourceTemplates;
        public DGObjectTemplate dGObjectTemplate;
        public Capacity()
        {
            domains = new List<DomainBase>();
            entities = new List<DGEntityStruct>();
            halfStructTemplates = new List<HalfStructTemplate>();
            fileTemplates = new List<FileTemplate>();
            d2ResourceTemplates = new List<D2ResourceTemplate>();
            d3ResourceTemplates = new List<D3ResourceTemplate>();
        }
        public void Merge(Capacity capacity)
        {
            //capacity.domains.ForEach(
            //    x =>
            //    {
            //        if (domains.Where(y => y == x).FirstOrDefault() == null)
            //        {
            //            domains.Add(x);
            //        }
            //    });
            capacity.entities.ForEach(
                x =>
                {
                    if (entities.Where(y => y.EntityTypeCode == x.EntityTypeCode).FirstOrDefault() == null)
                    {
                        entities.Add(x);
                    }
                });
        }
        public static Capacity getInstance()
        {
            Capacity capacity = new Capacity();
            Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
            //结构化模板获取
            foreach (Assembly assembly in assemblys)
            {
                Type[] types = assembly.GetTypes();
                foreach (Type typeInfo in types)
                {
                    if (!typeInfo.IsSubclassOf(typeof(DGEntity)))
                    {
                        continue;
                    }

                    //转移到基类实现去

                    List<DGEntityStruct> res = (Activator.CreateInstance(typeInfo) as DGEntity).GetCapacity();
                    res.ForEach(
                        y =>
                        {
                            //if (capacity.domains.Where(x => x == y.EntityDomain).FirstOrDefault() == null)
                            //{
                            //    capacity.domains.Add(y.EntityDomain);
                            //}
                            capacity.entities.Add(y);
                        });
                }
            }

            return capacity;
        }
    }
}
